<template>
  <div class="view">
    <el-row class="handle-box" :model="filters">
      <el-col :span="10">
        <el-button class="jianbianbule" type="primary" icon="el-icon-plus" @click="handleAdd">{{
          $t("blockcon.block")
        }}</el-button>
      </el-col>
      <el-col :span="14" style="text-align: right;">
        <span>{{ $t("blockcon.areaname") }}：</span>
        <el-select
          v-model="filters.area"
          filterable
          clearable
          @change="search"
          :placeholder="$t('public.select')"
        >
          <el-option
            v-for="item in options"
            :key="item.areaId"
            :label="item.areaName"
            :value="item.areaId"
          >
          </el-option>
        </el-select>
        <span>{{ $t("blockcon.blockName") }}：</span>
        <el-input
          v-model="filters.blockName"
          @keyup.native.enter="search"
          :placeholder="$t('public.text')"
          clearable
        ></el-input>
        <el-button
          class="seacthsbtn"
          type="primary"
          id="btn"
          @click="search"
          icon="el-icon-search"
          >{{ $t("public.search") }}</el-button
        >
      </el-col>
    </el-row>
    <div class="tablebox">
      <template>
        <el-table :data="tables" size="mini" stripe class="tablefisbox">
          <el-table-column
            type="index"
            :label="$t('public.number')"
            width="80"
          ></el-table-column>
          <el-table-column
            prop="area"
            :label="$t('blockcon.area')"
          ></el-table-column>
          <el-table-column
            prop="blockName"
            :label="$t('blockcon.blockName')"
            width="180"
          ></el-table-column>
          <el-table-column
            prop="blockEnName"
            :label="$t('blockcon.blockEnName')"
          ></el-table-column>
          <el-table-column
            prop="shortName"
            :label="$t('blockcon.shortName')"
          ></el-table-column>
          <el-table-column prop="sort" :label="$t('areacon.sort')">
          </el-table-column>
          <el-table-column
            prop="createUserName"
            :label="$t('public.createUser')"
          ></el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('public.createTime')"
            :formatter="carTimeFilter"
          >
          </el-table-column>
          <el-table-column :label="$t('public.operation')" width="250">
            <template slot-scope="scope">
              <el-button
                type="warning"
                icon="el-icon-edit"
                round
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                >{{ $t("public.edit") }}</el-button
              >
              <el-button
                type="danger"
                icon="el-icon-delete"
                round
                size="mini"
                @click="handleDel(scope.$index, scope.row)"
                >{{ $t("public.del") }}</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>

      <!--工具条-->
      <el-col class="pages">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="pageSize"
          :total="total"
        >
        </el-pagination>
      </el-col>

      <!--编辑界面-->
      <el-dialog
        :title="textMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
        :close-on-click-modal="false"
      >
        <el-form :model="editForm" :rules="editFormRules" ref="editForm">
          <el-form-item :label="$t('blockcon.area')" prop="area">
            <el-select
              v-model="editForm.area"
              clearable
              :placeholder="$t('public.select')"
            >
              <el-option
                v-for="item in options"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item :label="$t('blockcon.blockName')" prop="blockName">
            <el-input v-model="editForm.blockName"></el-input>
          </el-form-item>
          <el-form-item :label="$t('blockcon.blockEnName')" prop="blockEnName">
            <el-input v-model="editForm.blockEnName"></el-input>
          </el-form-item>
          <el-form-item :label="$t('blockcon.shortName')" prop="shortName">
            <el-input v-model="editForm.shortName"></el-input>
          </el-form-item>
          <el-form-item :label="$t('areacon.sort')" prop="sort">
            <el-input
              oninput="this.value=this.value.replace(/[^\.\d]/g,'');this.value=this.value.replace('.','');"
              v-model="editForm.sort"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="dialogFormVisible = false" type="success">{{
            $t("public.cancelbtn")
          }}</el-button>
          <el-button
            v-if="dialogStatus == 'create'"
            type="primary"
            @click="createData"
            >{{ $t("public.savebtn") }}</el-button
          >
          <el-button v-else type="primary" @click="updateData">{{
            $t("public.changebtn")
          }}</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogStatus: "", //编辑框界面切换
      textMap: {
        //添加与编辑界面
        update: this.$t("public.edit"),
        create: this.$t("public.create"),
      },
      dialogFormVisible: false, //弹窗
      filters: {
        area: "",
        blockName: "",
      },
      tables: [], //表格数据
      total: 0, //总共条数
      page: 1, //请求页
      limit: 10, //页条数
      pageSize: 1, //当前页
      //是否必填项
      editFormRules: {
        area: [{ required: true, message: "请选择所属区域", trigger: "blur" }],
        blockName: [
          { required: true, message: "请输入区块名称", trigger: "blur" },
        ],
        blockEnName: [
          { required: true, message: "请输入英文名称", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输排序号", trigger: "blur" }],
      },
      //编辑界面数据
      editForm: {
        area: "",
        blockName: "",
        blockEnName: "",
        shortName: "",
        sort: "",
      },

      //区域名称
      options: [],
    };
  },
  watch: {
    "filters.blockName"(val) {
      if (val.trim() === "") {
        this.getBlock();
      }
    },
  },
  mounted() {
    this.getBlock();
    this.areaSelect();
  },
  methods: {
    handleSizeChange(val) {
      this.limit = val;
      this.getBlock();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.pageSize = val;
      this.getBlock();
    },
    //编辑参数
    handleEdit(index, row) {
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.editForm = {
        blockId: row.blockId,
        area: row.areaId,
        blockName: row.blockName,
        blockEnName: row.blockEnName,
        shortName: row.shortName,
        sort: row.sort,
        createUser: JSON.parse(this.$store.state.userInfo).uid,
      };
    },
    //新增参数
    handleAdd() {
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.editForm = {
        area: "",
        blockName: "",
        blockEnName: "",
        shortName: "",
        sort: "",
        createUser: JSON.parse(this.$store.state.userInfo).uid,
      };
    },
    //模糊搜索
    search() {
      this.page = 1;
      this.limit = 10;
      this.pageSize = 1;
      this.getBlock();
    },
    //获取
    async getBlock() {
      try {
        let params = {
          Area: this.filters.area,
          BlockName: this.filters.blockName,
          page: this.page,
          limit: this.limit,
        };
        let res = await this.$api.set.getBlock(params);
        this.total = res.count;
        this.tables = res.data;
      } catch (e) {
        console.log(e);
      }
    },
    //获取区域名称数据
    async areaSelect() {
      let option = await this.$api.set.areaSelect();
      this.options = option.data;
    },
    //添加
    createData() {
      this.$refs.editForm.validate(async (valid) => {
        if (valid) {
          let params = Object.assign({}, this.editForm);
          let res = await this.$api.set.addBlock(params);
          if (res.code == 200) {
            this.$message({
              showClose: true,
              message: this.$t("public.addsuccess"),
              type: "success",
            });
            this.$refs["editForm"].resetFields(); //清空表单
            this.dialogFormVisible = false; //关闭弹窗
            this.getBlock(); //刷新数据
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        }
      });
    },
    //修改
    updateData() {
      this.$refs.editForm.validate(async (valid) => {
        if (valid) {
          let params = Object.assign({}, this.editForm);
          let res = await this.$api.set.editBlock(params);
          if (res.code == 200) {
            this.$message({
              showClose: true,
              message: this.$t("public.editsuccess"),
              type: "success",
            });
            this.$refs["editForm"].resetFields(); //清空表单
            this.dialogFormVisible = false;
            this.getBlock(); //刷新数据
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        }
      });
    },
    //删除
    handleDel(index, row) {
      // 二次确认删除
      this.$confirm(
        this.$t("public.confirmcontent"),
        this.$t("public.confirmtitle"),
        {
          type: "warning",
        }
      )
        .then(async () => {
          // this.tables.splice(index, 1);
          let params = {
            id: row.blockId,
            usid: JSON.parse(this.$store.state.userInfo).uid,
          };
          let res = await this.$api.set.removeBlock(params);
          if (res.code == 200) {
            this.$message.success(this.$t("public.confirmtips1"));
            this.getBlock(); //刷新数据
          } else {
            this.$message({
              showClose: true,
              message: res.msg,
              type: "error",
            });
          }
        })
        .catch(() => {});
    },
    carTimeFilter(row, column, cellValue, index) {
      const daterc = row[column.property];
      if (daterc != null) {
        return this.$moment(daterc).format("YYYY-MM-DD HH:mm:ss");
      }
    },
  },
};
</script>